package com.lyl.syt.controller;/**
 * @author Mr.Luo
 * @date 2022-04-24 15:16
 */

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.syt.common.result.Result;
import com.lyl.syt.common.utils.MD5;
import com.lyl.syt.model.hosp.HospitalSet;
import com.lyl.syt.modules.mp.service.HospitalSetService;
import com.lyl.syt.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author Mr.Luo
 * @date 2022-04-24 15:16
 * @desc
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@RequiredArgsConstructor
@Api(tags = "医院设置")
public class HospitalSetController {
    /**
     * 注入service
     */
    private final HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        //调用service的方法
        return Result.ok(hospitalSetService.list());
    }

    //2 逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result<Boolean> removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "分页-获取所有医院设置")
    @PostMapping("/page/{page}/{size}")
    public Result<Page<HospitalSet>> findPage(@PathVariable("page") Long page,
                                              @PathVariable("size") Long size,
                                              @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        LambdaQueryChainWrapper<HospitalSet> wrapper = new LambdaQueryChainWrapper<>(hospitalSetService.getBaseMapper());
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        Page<HospitalSet> pageHospitalSet = wrapper.like(StrUtil.isNotBlank(hosname), HospitalSet::getHosname, hosname)
                .eq(StrUtil.isNotBlank(hoscode), HospitalSet::getHoscode, hoscode)
                .page(new Page<>(page, size));
        return Result.ok(pageHospitalSet);
    }

    //4 添加医院设置
    @PostMapping("saveHospitalSet")
    @ApiOperation(value = "添加医院设置")
    public Result<Boolean> saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    @ApiOperation(value = "根据id获取医院设置")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @PostMapping("updateHospitalSet")
    @ApiOperation(value = "修改医院设置")
    public Result<Boolean> updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除医院设置")
    public Result<Boolean> batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    @ApiOperation(value = "医院设置锁定和解锁")
    public Result<Boolean> lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    @ApiOperation(value = "发送签名秘钥")
    public Result<Boolean> lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }






}
